package com.ryan.route.controller;


import com.ryan.route.entity.DestCategory;
import com.ryan.route.service.DestCategoryService;
import com.ryan.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 旅游目的地分类 前端控制器
 * </p>
 *
 * @author ryan
 * @since 2020-07-26
 */
@RestController
@RequestMapping("/route/category")
public class DestCategoryController {

    /**
     * 通过excel添加分类
     */
    @Autowired
    DestCategoryService categoryService;

    @PostMapping("/addSubjectByExcel")
    public R addSubjectByExcel(MultipartFile file){
        categoryService.addSubjectByExcel(file, categoryService);
        return R.ok();
    }

    /**
     * 获取树状分类数据
     * @return
     */
    @GetMapping("/list/tree")
    public R getListTree(){
        List<DestCategory> list =  categoryService.listWithTree();
        return R.ok().data("list", list);
    }

    /**
     * 增加分类节点
     * @param category
     * @return
     */
    @PostMapping("/addCategory")
    public R addCategory(@RequestBody DestCategory category){
        boolean save = categoryService.save(category);
        return save?R.ok():R.error();
    }

    /**
     * 通过id获取分类节点信息
     * @param id
     * @return
     */
    @GetMapping("/getCategoryById/{id}")
    public R getCategoryById(@PathVariable String id){
        DestCategory category = categoryService.getById(id);
        return R.ok().data("category", category);
    }

    /**
     * 修改分类节点信息
     * @param category
     * @return
     */
    @PostMapping("/updateCategory")
    public R updateCategory(@RequestBody DestCategory category){
        boolean update = categoryService.updateById(category);
        return update?R.ok():R.error();
    }

    /**
     * 删除分类节点
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R deleteCategory(@RequestBody Long[] ids){
        boolean delete = categoryService.removeByIds(Arrays.asList(ids));
        return delete?R.ok():R.error();
    }


}

